package com.t.bricks.bussiness.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * XSimpleUser模块的拦截器
 */
public class SimpleMonitorInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object arg2) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
				
		String servletPath = request.getServletPath();

		//--未得到路径信息,与本拦截器无关,不拦截--//
		if (null == servletPath) {
			return true;//通过
		}
		
		//--没有本模块控制层的路径则与本拦截器无关,不拦截--//
		if (servletPath.indexOf("/simpleuser/") < 0) {
			return true;//通过
		}
		
		Object obj = request.getSession().getAttribute("simpleUser");
		if (obj == null) {//没有进行登录,则不允许通过
			response.sendRedirect("/login/");//跳转到登录页
			return false;
		}
		
		return true;
	}
	
}